
package com.spring.demo.redis.controller;


import com.spring.demo.redis.dao.UserDao;
import com.spring.demo.redis.utils.RedisLock;
import com.spring.demo.redis.utils.RedisUtil;
import com.spring.demo.redis.utils.RedissonUtil;
import com.spring.demo.redis.vo.User;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/redis")
public class RedisController {
    @Autowired
    public RedisUtil redis;
    @Autowired
    private RedisLock redisLock;

    @Autowired
    public UserDao dao;

    @RequestMapping("/test/{id}")
    public Object test(@PathVariable int id) {

        String str = "user" + id;
        Object name = redis.getValue(String.valueOf(id));

        if (name != null) {
            System.out.println("redis1---" + name);
            return "ok-redis1---";
        }
        RLock lock = redissonClient.getLock(String.valueOf(str));
        try {
            Boolean b = lock.tryLock(3, TimeUnit.SECONDS);
            if (b) {
                Thread.sleep(2000);
                name = redis.getValue(String.valueOf(id));
                if (name != null) {
                    System.out.println("redis2---" + name);
                    lock.unlock();
                    return "ok-redis2---------------------------------";
                }
                User customer = dao.queryByIdOne(id);
                redis.setKey(id + "", customer.getName(), (long) 120, TimeUnit.SECONDS);

                System.out.println("database---------" + customer.getName() + customer.getId());
                lock.unlock();
                return "ok-database--";
            } else {
                name = redis.getValue(String.valueOf(id));
                if (name != null) {
                    System.out.println("redis3----------" + name);
                    return "ok-redis3--";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "------数据库 空-----";
    }


    @RequestMapping("/t2")
    public String test2(String id) {

        Boolean lock = redisLock.lock(id, 20L, TimeUnit.SECONDS);
        if (lock) {
            System.out.println("锁了-可以执行业务代码了");
            redis.incr("sum");
            redisLock.unlock(id);
            return "成功";
        }
        return "失败";
    }

    @Autowired
    private RedissonUtil redissonUtil;

    @Autowired
    private RedissonClient redissonClient;

    @RequestMapping("/t3")
    public String test3() {
        String sum = "sum";

        RLock lock = redissonClient.getLock("zzz");
        System.out.println(Thread.currentThread().getName() + "开始");

        boolean b = false;
        try {
            b = lock.tryLock(3, TimeUnit.SECONDS);
            if (b) {
                if (redis.getValue(sum) != null) {
                    int n = Integer.parseInt(String.valueOf(redis.getValue(sum))) + 1;
                    redis.setKey(sum, n);
                }
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName() + "获得");
                lock.unlock();
            } else {
                System.out.println(Thread.currentThread().getName() + "不获得");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return "ok";

    }


}
